Android极速开发一(Apk安装) 发表于 2016-08-27 | 分类于 Android , Android极速开发 | | 阅读次数 安装之前判断是否有root权限,如果有root权限就静默安转,如果没有就利用意图进行安装 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232/** * * @author Javen * @since 2016/05/24 */public class ApkController { /** * 描述: 安装 */ public static boolean install(String apkPath,Context context){ // 先判断手机是否有root权限 if(hasRootPerssion()){ // 有root权限,利用静默安装实现 return clientInstall(apkPath); }else{ // 没有root权限,利用意图进行安装 File file = new File(apkPath); if(!file.exists()) return false; Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive"); context.startActivity(intent); return true; } } /** * 描述: 卸载 */ public static boolean uninstall(String packageName,Context context){ if(hasRootPerssion()){ // 有root权限,利用静默卸载实现 return clientUninstall(packageName); }else{ Uri packageURI = Uri.parse("package:" + packageName); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI); uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(uninstallIntent); return true; } } /** * 判断手机是否有root权限 */ private static boolean hasRootPerssion(){ PrintWriter PrintWriter = null; Process process = null; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } return false; } /** * 静默安装 */ private static boolean clientInstall(String apkPath){ PrintWriter PrintWriter = null; Process process = null; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); PrintWriter.println("chmod 777 "+apkPath); PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib"); PrintWriter.println("pm install -r "+apkPath);// PrintWriter.println("exit"); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } return false; } /** * 静默卸载 */ private static boolean clientUninstall(String packageName){ PrintWriter PrintWriter = null; Process process = null; try { process = Runtime.getRuntime().exec("su"); PrintWriter = new PrintWriter(process.getOutputStream()); PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib "); PrintWriter.println("pm uninstall "+packageName); PrintWriter.flush(); PrintWriter.close(); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } return false; } /** * 启动app * com.exmaple.client/.MainActivity * com.exmaple.client/com.exmaple.client.MainActivity */ public static boolean startApp(String packageName,String activityName){ boolean isSuccess = false; String cmd = "am start -n " + packageName + "/" + activityName + " \n"; Process process = null; try { process = Runtime.getRuntime().exec(cmd); int value = process.waitFor(); return returnResult(value); } catch (Exception e) { e.printStackTrace(); } finally{ if(process!=null){ process.destroy(); } } return isSuccess; } private static boolean returnResult(int value){ // 代表成功 if (value == 0) { return true; } else if (value == 1) { // 失败 return false; } else { // 未知情况 return false; } } /** * 查询所有非系统app的信息 * @param mContext * @return */ public static List<Map<String, Object>> getAPPInstalled(Context mContext) { List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); // 获取系统内的所有程序信息 Intent mainintent = new Intent(Intent.ACTION_MAIN, null); mainintent.addCategory(Intent.CATEGORY_LAUNCHER); List<PackageInfo> packageinfo = mContext.getPackageManager() .getInstalledPackages(0); int count = packageinfo.size(); for (int i = 0; i < count; i++) { PackageInfo pinfo = packageinfo.get(i); ApplicationInfo appInfo = pinfo.applicationInfo; if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) { // 系统程序 忽略 } else { // 非系统程序 Map<String, Object> map = new HashMap<String, Object>(); map.put("app_logo", pinfo.applicationInfo.loadIcon(mContext .getPackageManager())); map.put("app_name", pinfo.applicationInfo.loadLabel(mContext .getPackageManager())); map.put("package_name", pinfo.applicationInfo.packageName); map.put("app_version_name", pinfo.versionName); map.put("app_version_code", pinfo.versionCode); listItems.add(map); } } return listItems; } /** * 判断应用是否需要安装 * @param mContext * @param packageName * @param versionCode * @return */ public static boolean isInstalled(Context mContext,String packageName,int versionCode) { // 获取系统内的所有程序信息 Intent mainintent = new Intent(Intent.ACTION_MAIN, null); mainintent.addCategory(Intent.CATEGORY_LAUNCHER); List<PackageInfo> packageinfo = mContext.getPackageManager() .getInstalledPackages(0); int count = packageinfo.size(); String pn; int vc; for (int i = 0; i < count; i++) { PackageInfo pinfo = packageinfo.get(i); ApplicationInfo appInfo = pinfo.applicationInfo; if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) { // 系统程序 忽略 } else { // 非系统程序 pn=pinfo.applicationInfo.packageName; vc=pinfo.versionCode; if (pn.equalsIgnoreCase(packageName) && vc >= versionCode) { return true; } } } return false; }} 欢迎您扫一扫上面的微信公众号,订阅我的博客! 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏